package com.sgcc.pda.hzaidl.impl;

import android.bluetooth.BluetoothDevice;
import android.os.RemoteException;

import com.sgcc.pda.hw.ReturnResult;
import com.sgcc.pda.hzaidl.IHZAidlCallback;
import com.sgcc.pda.hzaidl.entity.BluetoothConnectParam;

import com.sgcc.pda.hzaidl.entity.BluetoothDiscoveryParam;
import com.sgcc.pda.hzaidl.entity.ResponseBean;
import com.sgcc.pda.tools.JsonUtils;

import java.util.Set;

/**
 * created by tsinling on: 2020-03-03 11:52
 * description:
 */
public class BluetoothManager {


    public static boolean isConnected() throws RemoteException {
        ReturnResult returnResult= HardWareUtil.requestHardwareData("bluetooth_isConnected");
        return returnResult != null && returnResult.getCode() == 0;
    }



    public static void disConnect() throws RemoteException {
       HardWareUtil.requestHardwareData("bluetooth_disConnect");
    }


    public static void connect(final String macAddress, final OnBluetoothConnectListener callback) throws RemoteException {
        final BluetoothConnectParam.Request param = new BluetoothConnectParam.Request(macAddress);
        HardWareUtil.requestHardwareData("bluetooth_connect", JsonUtils.toJson(param),new IHZAidlCallback.Stub() {
            @Override
            public void callback(String result) throws RemoteException {
                ResponseBean responseBean = JsonUtils.toBean(result, ResponseBean.class);
                BluetoothConnectParam.Response response = JsonUtils.toBean(responseBean.getMsgJson(), BluetoothConnectParam.Response.class);
                if (null == response) {
                    onConnect(false, -1, "硬件服务返回为空");
                }
                //  // fixme 因为sdk里 蓝牙断开连接也会回调，且为fasle，所以这里细分，若是连接失败则弹框，要是断开连接则toast
                if (response.isDisConnect()) {
                    onDisConnect(response.getCode(), response.getMsg());

                } else {
                    onConnect(response.isSuccess(), response.getCode(), response.getMsg());
                }
            }
            void onConnect(boolean success, int code, String msg) {
                if (null != callback) {
                    callback.onConnect(success, code, msg);
                }
            }

            /**
             *  蓝牙断开连接
             * @param code
             * @param msg
             */
            void onDisConnect(int code, String msg) {
                if (null != callback) {
                    callback.onDisConnect(code, msg);
                }
            }
        });
    }

    /**
     *  开始蓝牙搜索,蓝牙搜索默认 12 秒.
     * @param callback 蓝牙搜索回调
     */
    public static void startDiscovery(final OnBluetoothDiscoverListener callback) throws RemoteException {
        startDiscovery(0,callback);

    }

    /**
     *
     * @param timeout 若是<=0  表示采用系统默认值,不进行设置
     * @param callback
     */
    public static void startDiscovery(int timeout,final OnBluetoothDiscoverListener callback) throws RemoteException {
        // 蓝牙搜索时间,暂时未用到.
        final BluetoothDiscoveryParam.Request param = new BluetoothDiscoveryParam.Request(timeout);
       HardWareUtil.requestHardwareData("bluetooth_startDiscovery", JsonUtils.toJson(param), new IHZAidlCallback.Stub() {

           @Override
           public void callback(String result) throws RemoteException {
               ResponseBean responseBean = JsonUtils.toBean(result, ResponseBean.class);
               BluetoothDiscoveryParam.Response response = JsonUtils.toBean(responseBean.getMsgJson(), BluetoothDiscoveryParam.Response.class);
               if (null != callback) {
                   int methodCode = response.getCallbackMethd();
                   switch (methodCode) {
                       case BluetoothDiscoveryParam.Response.ON_BONDED_DEVICES:
                           callback.onBondedDevices(response.getPairedBluetoothDevices());
                           break;
                       case BluetoothDiscoveryParam.Response.ON_FOUND:
                           callback.onFound(response.getDevice());
                           break;
                       case BluetoothDiscoveryParam.Response.ON_DISCOVERY_STARTED:
                           callback.onDiscoveryStarted();
                           break;
                       case BluetoothDiscoveryParam.Response.ON_DISCOVERY_FINISHED:
                           callback.onDiscoveryFinished();
                           break;

                   }
               }
           }
       });

    }

    /**
     *  停止搜索蓝牙
     * @return
     */
    public static boolean cancelDiscovery() throws RemoteException {
        ReturnResult returnResult = HardWareUtil.requestHardwareData("bluetooth_cancelDiscovery");
        return returnResult != null && returnResult.getCode() == 0;
    }

    public interface OnBluetoothConnectListener {
        /**
         * @param success 是否连接成功
         * @param code    响应码
         * @param msg     提示信息
         */
        void onConnect(boolean success, int code, String msg);

        /**
         * 蓝牙断开连接
         *
         * @param code
         * @param msg
         */
        void onDisConnect(int code, String msg);
    }

    public interface OnBluetoothDiscoverListener {
        /**
         * 已经配过对的
         *
         * @param pairedBluetoothDevices
         */
        void onBondedDevices(Set<BluetoothDevice> pairedBluetoothDevices);

        /**
         * 新发现单位
         *
         * @param newDevice
         */
        void onFound(BluetoothDevice newDevice);

        /**
         * 蓝牙搜索开始
         */
        void onDiscoveryStarted();

        /**
         * 蓝牙搜索结束
         */
        void onDiscoveryFinished();
    }

}
